package com.kaifamiao.www.demo.do0810;

public class NodeList {
    //链表的头
    private Node head;
    //链表的尾
    private Node last;
    //记录链表的长度
    private int length;

    public boolean add(String value){
        //创建一个ode对象
        Node node = new Node(value);
        //判断链表中是否有元素
        if (head == null){
            //链表没有元素，把当前node设置为链表头
            head = node;
            //last = node;
        }else {
            last.next = node;
            //last = node;
            //当前指向的节点
//            Node currentNode = head;
//            //找到列表的最后一个元素
//            while (currentNode.next !=null){
//                currentNode = currentNode.next;
//            }
//            //把当前的node对象设置为链表的最后一个元素
//            currentNode.next = node;
        }
        last = node;
        length++;
        return true;
    }

    public String remove(int index){
        checkIndex(index);
        //被删除元素current
        Node current = null;
        if (index == 0){
            //被删的元素
            current = head;
            head = head.next;
        }else{
            //找被删元素前一个元素 prev
            Node prev = head;//index = 0;
            for (int i = 1;i <index;i++){
                prev = prev.next;
            }
            current = prev.next;
            //被删元素的下一个元素
            Node next = current.next;
            //
            prev.next = next;
        }
        length --;
        return current.value;
    }

    public String set(int index,String value){
        checkIndex(index);
        Node current = head;//index=0
        for (int i = 1;i <= index; i++){
            current = current.next;
        }
        String old = current.value;
        current.value = value;
        return old;
    }

    public boolean  add(int index,String value){
        checkLndex0(index);
        Node add = new Node(value);
        if (index == 0){
            add.next = head;
            head = add;
        }else if (index == length){
//            last.next = add;
//            last = add;
            add(value);
        }else{
            Node prev = head;
            for (int i =1;i<index;i++){
                prev = prev.next;
            }
            Node current = prev.next;
            prev.next = add;
            add.next = current;
        }
        length++;
        return true;
    }
    private void checkLndex0(int index){
        if (index < 0 || index > length){
            throw new NodeListIndexOutOfBoundsException("索引异常， index 应该是 0 到 " + length + "。但是接收到的是 " + index );
        }

    }

    public String get(int index){
        checkIndex(index);
        //index = 0时
        Node current = head;
        for (int i = 1;i <= index;i++){
            current = current.next;
        }
        return current.value;
    }

    public int size(){
        return length;
    }
    //检查下标是否在正确范围内
    private void checkIndex(int index){
        if (index < 0 || index >=length){
            throw new NodeListIndexOutOfBoundsException("索引异常,index应该是0到"+(length-1)+"。但是接收到的是"+index);
        }
    }
    private class NodeListIndexOutOfBoundsException extends IndexOutOfBoundsException{
        public NodeListIndexOutOfBoundsException(String msg){
            super(msg);
        }
    }
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder("NodeList{");
        if (head != null){
            Node current = head;
            while (current != null){
               // sb.append(current.value+",");
                sb.append(current+",");
                current = current.next;
            }
        }
        String str = sb.substring(0,sb.length()-1);
        str+="}";
        return str;
    }
    /**
     * 链表中的每一个元素（节点）都是一个Node对象
     */
    private class Node{
        //节点的值
        private String value;

        private Node next;

        public Node(String value){
            this.value = value;
        }
        @Override
        public String toString(){
            return this.value;
        }
    }

}
